/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.dialog;

import org.json.JSONObject;
import org.webos.java.base.Application;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@DialogType(jsClass="core.QuestionDialog")
public class QuestionDialog extends Dialog{

    private int selectedButton = 0;

    public QuestionDialog(Application application, String aQuestion) {
        this(application, aQuestion, new String[]{"Ok", "Cancel"}, 1);
    }
    public QuestionDialog(Application application, String aQuestion, 
            String[] aButtons, int aDefault) {
        super(application);
        getData().putOpt("question", aQuestion);
        getData().putOpt("buttons", aButtons);
        getData().putOpt("defaultButton", aDefault);
        selectedButton = aDefault;
    }

    public synchronized int ask(){
        show();
        try {
            wait();            
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return selectedButton;
    }

    @RemoteMessage
    public void buttonClick(JSONObject aData){
        int button = aData.optInt("button", 0);
        selectedButton = button;
        closeDialog(aData);
    }

    @RemoteMessage
    @Override
    synchronized public void closeDialog(JSONObject data) {
        super.closeDialog(data);
        notify();
    }


}
